﻿function AccountViewModel(model) {
    var selfAccount = this;

    selfAccount.Main = mainViewModel;
    selfAccount.Login = ko.observable();
    selfAccount.Signup = ko.observable();

    if (model) {
        selfAccount.Login(new LoginViewModel(model.Login));
        selfAccount.Signup(new SignupViewModel(model.Signup));
    }
    else {
        selfAccount.Login(new LoginViewModel());
        selfAccount.Signup(new SignupViewModel());
    }

    selfAccount.AttempLogin = function () {
        if (selfAccount.Main.IsValidModel(selfAccount.Login())) {
            comswing.ShowLoading();
            $.ajax({
                url: $("#login").attr("action"),
                type: 'POST',
                data: ko.toJSON(selfAccount),
                contentType: 'application/json',
                success: function (result) {
                    if (!result.Success) {
                        selfAccount.Login().FailedSubmit(true);
                        comswing.HideLoading(function () { comswing.Message(result.Message, 1) });
                    }
                    else
                        if (result.MustRedirect)
                            window.location.href = result.RedirectURL;
                }
            });
        }
        else {
            selfAccount.Login().FailedSubmit(true);
            comswing.Message(formErrorMessage, 1);
        }
    }

    selfAccount.Signup = function () {
        alert("Signup action");
    }
}